//
//  WaittingCommentDetailViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/23.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "WaittingCommentDetailViewController.h"
#import "PayViewController.h"
#import "WaitIndentDetailTableViewCell.h"
#import "CommitCommentViewController.h"
static NSString * cellIdentifier = @"WaitIndentDetailTableViewCell";
@interface WaittingCommentDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UIView *payInfoView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UILabel *reciverName;
@property (strong, nonatomic) IBOutlet UILabel *reciverPhone;
@property (strong, nonatomic) IBOutlet UILabel *reciverAddress;


@property (strong, nonatomic) IBOutlet UILabel *indentNumber;
@property (strong, nonatomic) IBOutlet UILabel *payTime;
@property (strong, nonatomic) IBOutlet UILabel *ifNeedPaper;
@property (strong, nonatomic) IBOutlet UILabel *payType;



@property (strong, nonatomic) IBOutlet UILabel *drivePay;
@property (strong, nonatomic) IBOutlet UILabel *forSaleMoney;
@property (strong, nonatomic) IBOutlet UILabel *turePayMoney;
@property (strong, nonatomic) IBOutlet UIButton *commentButton;
@property (strong, nonatomic) IBOutlet UIButton *serviceButton;


@property(nonatomic,strong)NSMutableArray * goodsInfoArry;
@property(nonatomic,strong)NSString * storeName;

@end

@implementation WaittingCommentDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _goodsInfoArry = [NSMutableArray array];
    _storeName = @"";
    self.navgationTitleLabel.text = self.titleStr;
    if([self.titleStr isEqualToString:@"订单详情(待发货)"]){
        _commentButton.hidden = YES;
    }else if ([self.titleStr isEqualToString:@"待收货/使用"]){
        [_serviceButton setTitle:@"退货/退款" forState:UIControlStateNormal];
        [_commentButton setTitle:@"确认收货" forState:UIControlStateNormal];
    }
    [self initViewLayout];
    // Do any additional setup after loading the view.
    [self getIndentDetailInfo];
}


- (void)initViewLayout{
    [self adaptiveViewLayout: self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.payInfoView];
   
    
    _commentButton.layer.borderWidth = 1;
    _commentButton.layer.borderColor = [UIColor colorWithRed:1.000 green:0.647 blue:0.035 alpha:1.000].CGColor;
    _commentButton.layer.cornerRadius = 3*VerticalRatio();
    
    
    _serviceButton.layer.borderWidth = 1;
    _serviceButton.layer.borderColor = [UIColor colorWithWhite:0.608 alpha:1.000].CGColor;
    _serviceButton.layer.cornerRadius = 3*VerticalRatio();
    
    
    [self.tableView registerNib:[UINib nibWithNibName:cellIdentifier bundle:nil] forCellReuseIdentifier:cellIdentifier];
}
#pragma mark == 返回
- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}
#pragma mark == 申请服务
- (IBAction)serviceClick:(UIButton *)sender {
}
#pragma mark == 评论
- (IBAction)commentClick:(UIButton *)sender {
    if([sender.titleLabel.text isEqualToString:@"确认收货"]){
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        [dic setObject:_indentId forKey:@"orderId"];
        [dic setObject:@"5" forKey:@"mroStatus"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_updateOrderStatus withHttpType:@"POST" withTag:SX_updateOrderStatusTag];
    }else if ([sender.titleLabel.text isEqualToString:@"评价晒单"]){
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        CommitCommentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"CommitComment"];
        [self.navigationController   pushViewController:page animated:YES];
    }
}
#pragma mark == 获取订单详情
- (void)getIndentDetailInfo{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:_indentId forKey:@"orderId"];
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_saveOrderDetail withHttpType:@"POST" withTag:SX_SX_saveOrderDetail];
    
}
#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 128*VerticalRatio();
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 38*VerticalRatio();
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _goodsInfoArry.count;
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView * view = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 38), NO)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel * label =  [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(12,0, 265, 38), NO)];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor colorWithWhite:0.196 alpha:1.000];
    label.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];
    label.text = _storeName;
    [view addSubview:label];
    
    
    
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WaitIndentDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell= [[WaitIndentDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    NSDictionary * dic = _goodsInfoArry[indexPath.row];
    
    NSString * image = dic[@"appAdertImagUri"];
    if (image.length>0) {
        [cell.indentImage sd_setImageWithURL:[NSURL URLWithString:image]];
    }else{
        cell.indentImage.image =[UIImage imageNamed:@"cloth1"];
    }
    
    
    
    NSMutableAttributedString * Str = [[NSMutableAttributedString alloc]initWithString:@"优惠劵:已抵扣十元优惠劵"];
    [Str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.961 green:0.635 blue:0.161 alpha:1.000] range:NSMakeRange(0,4)];
    cell.indentForSale.attributedText = Str;
    
    
    
    cell.indentName.frame = flexibleFrame(CGRectMake(104, 39, 204, 20), NO);
    cell.indentName.numberOfLines = 0;
    cell.indentName.text = [NSString stringWithFormat:@"%@",dic[@"mgooName"]];
    [cell.indentName sizeToFit];
    
    
    cell.indentInfo.frame = flexibleFrame(CGRectMake(104, 70, 204, 20), NO);
    cell.indentInfo.numberOfLines = 0;
    cell.indentInfo.text = [NSString stringWithFormat:@"%@",dic[@"invoiceInformation"]];
    [cell.indentInfo sizeToFit];
    cell.indentNumber.text = [NSString stringWithFormat:@"x%@",dic[@"goodsCount"]];
    NSString * str = [NSString stringWithFormat:@"￥%@",[Common stringFromData:dic[@"mgooPublishPrice"]]];
    NSString * oldPrice = [NSString stringWithFormat:@"￥%@",[Common stringFromData:dic[@"mgooPublishPrice"]]];
    
    NSMutableAttributedString * price = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@     %@",str,oldPrice]];
    [price addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:0.612 alpha:1.000] range:NSMakeRange(price.length-oldPrice.length, oldPrice.length)];
    [price addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13*VerticalTexeRatio()] range:NSMakeRange(price.length-oldPrice.length, oldPrice.length)];
    [price addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(price.length-oldPrice.length, oldPrice.length)];
//    [price addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14*VerticalTexeRatio()] range:NSMakeRange(0, str.length)];
    
    cell.indentPrice.attributedText = price ;
    return cell;
}




#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    
    if (tag==SX_SX_CarCreateOrderTag) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:2];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            PayViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
            //            page.payInfoArray = _goodsInfoArray;
            page.payOrderName = @"凡人街在线商城";
            page.payOrderPrice = self.turePayMoney.text;
            NSArray *arr = database[@"result"];
            NSString *orderId = @"";
            for (NSDictionary *dic in arr) {
                orderId = dic[@"mgorNo"];
            }
            page.payId = orderId;
            
            [self.navigationController   pushViewController:page animated:YES];
            
        }
        
    }else if (tag == SX_SX_createPackFruitTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:2];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            PayViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
            page.payOrderName = @"凡人街在线商城";
            page.payOrderPrice = self.turePayMoney.text;
            page.payId = database[@"result"][@"mgorNo"];
            [self.navigationController   pushViewController:page animated:YES];
            
        }
        
    }else if (tag == SX_SX_saveOrderDetail){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            _goodsInfoArry = database[@"result"];
            
            _tableView.frame = flexibleFrame(CGRectMake(0, 265, 320, _goodsInfoArry.count*166), YES);
            _payInfoView.frame = flexibleFrame(CGRectMake(0, 265+CGRectGetHeight(_tableView.frame), 320, _payInfoView.frame.size.height), YES);
            _scroView.contentSize = flexibleSize(CGSizeMake(0, CGRectGetHeight(_tableView.frame)+CGRectGetHeight(_payInfoView.frame)+265), NO);
            
            
            _reciverName.text = _goodsInfoArry[0][@"msadReceiverName"];
            _reciverPhone.text = _goodsInfoArry[0][@"phone"];
            _reciverAddress.text = _goodsInfoArry[0][@"msadReceiverAddress"];
            _storeName = _goodsInfoArry[0][@"mbraStoreName"];
            
            NSString *time = [NSString stringWithFormat:@"%@",_goodsInfoArry[0][@"mgorDate"]];
            long long Time=[time longLongValue];
            NSDate *Date = [[NSDate alloc]initWithTimeIntervalSince1970:Time/1000.0];
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
            time = [formatter stringFromDate:Date];
            _payTime.text = time;
            
//            CGFloat money = 0;
//            for (int i = 0; i<_goodsInfoArry.count; i++) {
//                CGFloat price = [_goodsInfoArry[i][@"mgooPublishPrice"]floatValue];
//                money += price;
//            }
            CGFloat money = [_goodsInfoArry[0][@"totalCount"]floatValue];
            _turePayMoney.text = [NSString stringWithFormat:@"￥%.2f",money];

            _tableView.dataSource = self;
            _tableView.delegate = self;
            [_tableView reloadData];
        }
    }else if (tag == SX_updateOrderStatusTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [self.navigationController  popViewControllerAnimated:YES];
                [[CWHUDTips sharedInstance]  showTips:@"收货成功"  ];
          
            
        }
    }
    
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
